package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConst;
import com.itheima.entity.Result;
import com.itheima.service.MemberService;
import com.itheima.service.ReportService;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.List;
import java.util.Map;


@RequestMapping("/report")
@RestController
public class ReportController {
    @Reference
    MemberService memberService;
    @Reference
    ReportService reportService;
    @RequestMapping("/getMemberReport")
    public Result getMemberReport(){
        try {
            Map<String,Object> map=memberService.getMemberReport();
            return new Result(true, MessageConst.GET_MEMBER_NUMBER_REPORT_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConst.GET_MEMBER_NUMBER_REPORT_FAIL);
        }
    }
    @RequestMapping("/getSetmealReport")
    public Result getSetmealReport(){
        try {
            Map<String,Object>map=memberService.getSetmealReport();
            return new Result(true,MessageConst.GET_SETMEAL_COUNT_REPORT_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.GET_SETMEAL_COUNT_REPORT_FAIL);
        }
    }
    @RequestMapping("/getBusinessReportData")
    public Result getBusinessReportData(){
        try {
            Map<String, Object> map=reportService.getBusinessReportData();
            return new Result(true,MessageConst.GET_BUSINESS_REPORT_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.GET_BUSINESS_REPORT_FAIL);
        }
    }
    @RequestMapping("/exportBusinessReport")
    public Result exportBusinessReport(HttpServletResponse response) {
        try {
            Map<String, Object> map=reportService.getBusinessReportData();
            InputStream inputStream = this.getClass().getResourceAsStream("/template/report_template.xlsx");
            XSSFWorkbook xssfWorkbook=new XSSFWorkbook(inputStream);
            XSSFSheet sheet = xssfWorkbook.getSheetAt(0);
            XSSFRow row = sheet.getRow(2);
            XSSFCell cell = row.getCell(5);
            cell.setCellValue(""+map.get("reportDate"));
                row = sheet.getRow(4);
                 cell = row.getCell(5);
            cell.setCellValue(""+map.get("todayNewMember"));
            row = sheet.getRow(4);
                 cell = row.getCell(7);
            cell.setCellValue(""+map.get("todayNewMember"));
            row = sheet.getRow(5);
                 cell = row.getCell(5);
            cell.setCellValue(""+map.get("thisWeekNewMember"));
            row = sheet.getRow(5);
                 cell = row.getCell(7);
            cell.setCellValue(""+map.get("thisMonthNewMember"));
            row = sheet.getRow(7);
                 cell = row.getCell(5);
            cell.setCellValue(""+map.get("todayOrderNumber"));
            row = sheet.getRow(7);
                 cell = row.getCell(7);
            cell.setCellValue(""+map.get("todayVisitsNumber"));
            row = sheet.getRow(8);
                 cell = row.getCell(5);
            cell.setCellValue(""+map.get("thisWeekOrderNumber"));
            row = sheet.getRow(8);
                 cell = row.getCell(7);
            cell.setCellValue(""+map.get("thisWeekVisitsNumber"));
            row = sheet.getRow(9);
                 cell = row.getCell(5);
            cell.setCellValue(""+map.get("thisMonthOrderNumber"));
            row = sheet.getRow(9);
                 cell = row.getCell(7);
            cell.setCellValue(""+map.get("thisMonthVisitsNumber"));
            List<Map<String,Object>>hotSetmeals=(List<Map<String,Object>>)map.get("hotSetmeal");
           int num=12;
            for (Map<String, Object> hotSetmeal : hotSetmeals) {
                row = sheet.getRow(num);
                cell = row.getCell(4);
                cell.setCellValue(""+hotSetmeal.get("name"));
                cell = row.getCell(5);
                cell.setCellValue(""+hotSetmeal.get("setmeal_count"));
                cell = row.getCell(6);
                cell.setCellValue(""+hotSetmeal.get("proportion"));
                num++;
            }
            // 通过输出流进行文件下载
            ServletOutputStream out = response.getOutputStream();
            //设置输入的文件的类型
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("content-Disposition","attachment;filename="+"report.xlsx");
            xssfWorkbook.write(out);
            out.flush();
            out.close();
            xssfWorkbook.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
